﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using OpenGado.DATA;
using OpenGado.DATA.Entities;

namespace OpenGado.Win
{
    public partial class frmUnitManager : Form
    {
        public frmUnitManager()
        {
            InitializeComponent();
            GetUnitList();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            frmUnitOfMeasure frmUnits = new frmUnitOfMeasure();
            frmUnits.UpdateUnitList += new frmUnitOfMeasure.UpdateUnitOfMeasureHandler(GetUnitList);
            frmUnits.Show();
        }

        private void GetUnitList()
        {
            dgvUnitList.AutoGenerateColumns = false;
            IList<UnitOfMeasure> unitList = new List<UnitOfMeasure>();
            unitList = UnitOfMeasureDataControl.Instance.GetMeasures();
         
            
            dgvUnitList.DataSource = unitList;
            // manual bind 
            
           
            dgvUnitList.Columns[0].DataPropertyName = "Id";
            dgvUnitList.Columns[1].DataPropertyName = "Name";
            

        }

        private void dgvUnitList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            // open up for editing

            int unitId;

            if (dgvUnitList.CurrentRow != null)
            {
                int.TryParse(dgvUnitList.CurrentRow.Cells[0].Value.ToString(), out unitId);

                frmUnitOfMeasure frmUnits = new frmUnitOfMeasure(unitId);
                frmUnits.UpdateUnitList += new frmUnitOfMeasure.UpdateUnitOfMeasureHandler(GetUnitList);
                frmUnits.Show();
            }
               
        }

        private List<int> GetSelectedUnits()
        {
            List<int> selectedUnits = new List<int>();

            int unitId;

            foreach (DataGridViewRow rw in dgvUnitList.SelectedRows)
            {
                int.TryParse(rw.Cells[0].Value.ToString(), out unitId);
                selectedUnits.Add(unitId);
            }

            return selectedUnits;
        }

        private void DeleteRecord()
        {
            foreach (int i in GetSelectedUnits())
            {
                UnitOfMeasure u = new UnitOfMeasure();
                u.Id = i;
                UnitOfMeasureDataControl.Instance.Delete(u);
            }

            GetUnitList();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DialogResult res;

            res = MessageBox.Show("Are you sure you want to delete selected Item(s)", "Inventory", MessageBoxButtons.YesNo);

            if (res == DialogResult.Yes)
            {
                DeleteRecord();
            }
        }
    }
}
